#!/bin/bash
#
# $Id: get_df_info.sh 141 2011-03-24 09:13:02Z mamartynov@gmail.com $
#

. $OPENARS_HOME/common/debug.h || exit 1
. $OPENARS_HOME/common/functions.h || exit 1

user=$USER_ARS
[ -z "$user" ] && user=$LOGNAME
[ -z "$1" ] && { ERR "need parametr \"filename\""; exit 1; }
[ ! -f "$1" ] && { ERR "not find file \"$1\""; exit 1; }

srvlist=`cat $1`
#srvlist=`get_hosts --os=Unix --control=YES`
INF "count hosts: `echo $srvlist | wc -w`"

OUTFILE=$OUTDIR/get_df_info$(echo $(basename $1) | sed s/^srvlist// | sed s/txt//)out
>$OUTFILE

    for server in $srvlist ; do
	(
	result=""
	SSH $user@$server "df -k| grep -v \":\"|sed '1d'| awk '{if (NF == 1) {printf \$NF} else {print}}'| awk '{print \$1,\":\",\$2,\":\",\$3,\":\",\$5,\":\",\$6}'|grep -v \"/etc\"|grep -v \"cdrom\"|grep -v \"dmpfs\"|grep -v \"fd\"|grep -v \"swap\"|grep -v \"system\"|grep -v \"lib\"|grep -v \"proc\" |grep -v \"/devices\" |grep -v \"/mnt/\"" result
	ex_code="$?"
	if [ "$ex_code" -eq 255 ]; then
	    echo "not connected:0:0:0%:null" >> $TEMPDIR/$server.df
	    continue
	fi
    echo "$result" | sed 's/ //g' >> $TEMPDIR/$server.df
    
    while read line
    do
        device_name=`echo "$line" | awk -F":" '{print $1}'`
        totalspace=`echo "$line" | awk -F":" '{print $2}'`
        freespace=`echo "$line" | awk -F":" '{print $3}'`
        pfreespace=`echo "$line" | awk -F":" '{print $4}' | sed s/%$//`
        mountpoint=`echo "$line" | awk -F":" '{print $5}'`

        echo '  <device>' >> $TEMPDIR/$server.xout
        echo '   <hostname>'$server'</hostname>' >> $TEMPDIR/$server.xout
        echo '   <devname>'$device_name'</devname>' >> $TEMPDIR/$server.xout
        echo '   <totalspace>'$totalspace'</totalspace>' >> $TEMPDIR/$server.xout
        echo '   <freespace>'$freespace'</freespace>' >> $TEMPDIR/$server.xout
        echo '   <pfreespace>'$pfreespace'</pfreespace>' >> $TEMPDIR/$server.xout
        echo '   <mountpoint>'$mountpoint'</mountpoint>' >> $TEMPDIR/$server.xout
        echo '  </device>' >> $TEMPDIR/$server.xout
    done < $TEMPDIR/$server.df
    #echo ' </hostname>' >> $TEMPDIR/$server.xout
	) &

    done

WAIT 180

echo '<?xml version="1.0" encoding="UTF-8"?>' >> $OUTFILE
echo '<get_df_info>' >> $OUTFILE
for ofile in `ls $TEMPDIR/*.xout`
do
    cat $ofile >> $OUTFILE
done
echo '</get_df_info>' >> $OUTFILE

ECHO "$OUTFILE"

exit 0

